package com.playball.lottery.frm.yhzx;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.R;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.frm.set.SetMainActivity.AsyncLoader;
import com.playball.lottery.nt.GetRequestParams;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyHuiYuanRecarActivity extends Activity {
	private TextView tv_title;
	private TextView tv_back;
	EditText et_recharge_name,et_recharge_real_name,et_recharge_money,et_recharge_pw,et_recharge_note;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_myhuiyuan_recharge);
		tv_title = (TextView) findViewById(R.id.top_title_info);
		tv_title.setText("为下级充值");

		tv_back = (TextView) findViewById(R.id.top_title_back);
		tv_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}
		});
		
		et_recharge_name = (EditText) findViewById(R.id.et_recharge_name);
		et_recharge_real_name = (EditText) findViewById(R.id.et_recharge_real_name);
		et_recharge_money = (EditText) findViewById(R.id.et_recharge_money);
		et_recharge_pw = (EditText) findViewById(R.id.et_recharge_pw);
		et_recharge_note = (EditText) findViewById(R.id.et_recharge_note);
		
		
		Button btn_recharge = (Button) findViewById(R.id.btn_recharge);
		btn_recharge.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(et_recharge_money.getText().toString().isEmpty()){
					Toast.makeText(MyHuiYuanRecarActivity.this, "请输入下级会员的用户名！", Toast.LENGTH_LONG).show();
				}else if(et_recharge_real_name.getText().toString().isEmpty()){
					Toast.makeText(MyHuiYuanRecarActivity.this, "请输入下级会员的真实姓名！", Toast.LENGTH_LONG).show();
				}else if(et_recharge_money.getText().toString().isEmpty()){
					Toast.makeText(MyHuiYuanRecarActivity.this, "请输入充值金额！", Toast.LENGTH_LONG).show();
				}else if(et_recharge_pw.getText().toString().isEmpty()){
					Toast.makeText(MyHuiYuanRecarActivity.this, "请输入支付密码！", Toast.LENGTH_LONG).show();
				}else{
					AsyncLoader task = new AsyncLoader();
					task.execute("10211");
				}
				
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
				this.finish();
				return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	};
	
	private ShowTipDialog showdialog = new ShowTipDialog();
	public class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask
		protected void onPreExecute() {
			showdialog.showDialog(MyHuiYuanRecarActivity.this);
		}
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				JSONObject param = new JSONObject();
				param.put("SubUserName", et_recharge_name.getText().toString());
				param.put("realityname", et_recharge_real_name.getText().toString());// Password
				param.put("money", et_recharge_money.getText().toString());// Password
				param.put("password", et_recharge_pw.getText().toString());// Password
				param.put("note", et_recharge_note.getText().toString());// Password
				param.put("userid", ApplicationData.getInstance().getUserID());// Password
				String action = params[0];
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getApplicationContext());
				return strResult;
			} catch (Exception e) {
				Log.e("error", e.toString());
				return null;
			}
		}
		protected void onProgressUpdate(Integer... progress) {
		}
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						Toast.makeText(MyHuiYuanRecarActivity.this, "充值成功！", Toast.LENGTH_SHORT).show();
						MyHuiYuanRecarActivity.this.finish();
					} else {
						Toast.makeText(MyHuiYuanRecarActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException ex) {
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(MyHuiYuanRecarActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}
		protected void onCancelled() {
		}
	}
}
